// pages/information/information.js
const api = require('../../util')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        sex: [{name:'男',id: 1},{name:'女',id: 2}],
        nickname: '',
        active: '',
        date: '',
        birthday: '',
        region: [],
        addressid: [],
        end: '',
        head_img: '',
        head_imgs: ''
    },
    // 获取用户个人信息
    PersonalCenter() {
        var that = this
        api.http('/user/PersonalCenter', 'POST', false, {
        }, function (res) {
            if(res.code == 200) {
              if (res.data.province_id!=''&&res.data.city_id!='') {
                var region = ['',''],region_id=['','']
                region[0]=res.data.province_name
                region_id[0]=res.data.province_id
                region[1]=res.data.city_name
                region_id[1]=res.data.city_id
              } else {
                var region = []
              }
                var dates = ''
                if (res.data.birthday!='') {
                  dates = new Date().getFullYear() - res.data.birthday
                }
                that.setData({
                  nickname: res.data.nickname,
                  active: res.data.sex-1,
                  birthday: res.data.birthday,
                  date: dates,
                  head_img: res.data.head_img,
                  head_imgs: res.data.head_img,
                  region,
                  region_id
                })
            }
        })
    },
    // 昵称
    user(e) {
      this.setData({
        nickname:e.detail.value
      })
    },
    // 性别下拉
    sexChange(e) {
        var that = this
        this.setData({
          active: e.detail.value
        }, () => {
        })
    },
    // 日期下拉
    bindDateChange: function(e) {
        // 日期
        var myDate = new Date();
        var tYear = myDate.getFullYear();
        this.setData({
            date: tYear - e.detail.value,
            birthday: e.detail.value
        });
    },
    // 地址触发
    // bindRegionChange: function (e) {
    //   console.log(e)
    //     console.log('picker发送选择改变，携带值为', e.detail.value)
    //     this.setData({
    //       region: e.detail.value,
    //       addressid:  e.detail.code
    //     })
    // },
    selectstoreAddress: function (e) {
      var that = this
      that.setData({
        region: e.detail.region,
        region_id: e.detail.region_id,
      })
    },
    // 上传服务分类图片
    chooseimage_service: function (e) {
      var that = this;
      wx.chooseMedia({
        count: 1, // 默认9
        sizeType: ['original', 'compressed'], //可以指定是原图还是压缩
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) {
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          var image = 'teobj.image'
          wx.showLoading({
            title: '上传中...',
            mask: true
          })
          wx.uploadFile({
            url: app.globalData.url + '/common/uploadImage',
            filePath: res.tempFiles[0].tempFilePath,
            name: 'file',
            formData: {},
            success(res) {
              that.setData({
                head_img: JSON.parse(res.data).data.domain_name+JSON.parse(res.data).data.url,
                head_imgs: JSON.parse(res.data).data.url
              })
              wx.hideLoading();
            },
            fail: function (res) {
              wx.hideLoading()
              wx.showToast({
                title: res.msg,
                icon: 'none',
                duration: 1000
              })
            }
          })
        }
      })
    },
    // 保存
    btn() {
      var that = this
      if (this.data.nickname=='') {
        wx.showToast({
          title: '请输入昵称',
          icon: 'none',
          duration: 1000
        })
        return
      }
      if (this.data.active==-1) {
        wx.showToast({
          title: '请选择性别',
          icon: 'none',
          duration: 1000
        })
        return
      }
      if (this.data.birthday=='') {
        wx.showToast({
          title: '请选择出生年份',
          icon: 'none',
          duration: 1000
        })
        return
      }
      if (typeof(this.data.region_id)=='undefined') {
        wx.showToast({
          title: '请选择省、市',
          icon: 'none',
          duration: 1000
        })
        return
      }
      api.http('/user/editUserInfo', 'POST', true, {
        head_img: this.data.head_imgs,
        nickname: this.data.nickname,
        sex: Number(this.data.active)+1,
        birthday: this.data.birthday,
        province_id: this.data.region_id[0],
        city_id: this.data.region_id[1]
      }, function (res) {
          if(res.code == 200) {
              wx.showToast({
                title: res.msg,
                icon: 'success',
                duration: 1000
              })
              setTimeout(() => {
                wx.navigateBack({
                    delta: 1
                })
            }, 1000);
          } else {
              wx.showToast({
                title: res.msg,
                icon: 'none',
                duration: 1000
              })
          }
      })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
      var myDate = new Date();
      var tYear = myDate.getFullYear();
      this.setData({
          end: tYear
      });
      this.PersonalCenter()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    // onShareAppMessage() {

    // }
})